package com.ruoyi.system.coretools.mail;

import com.ruoyi.common.utils.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamSource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName: MailUtil
 * @Description: 邮件发送工具类
 */
@Slf4j

public class MailUtil {
    /**
     * 邮件发送器
     */
    private static JavaMailSender javaMailSender = SpringUtils.getBean(JavaMailSenderImpl.class);

    /**
     * 纯文本
     *
     * @param subject 标题
     * @param content 内容
     * @param from    发件人
     * @param to      收件人
     */
    public static void sendSimpleMail(String subject, String content, String from, String... to) {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom(from);
        mailMessage.setTo(to);
        mailMessage.setSubject(subject);
        mailMessage.setText(content);
        //发送邮件
        javaMailSender.send(mailMessage);
        log.info("邮件发送成功,subject:{},form:{},to:{}", subject, from, to);
    }


    /**
     * 图片
     *
     * @param subject
     * @param content 带有html代码
     * @param from
     * @param to
     * @throws MessagingException
     */
    public static void sendHtmlMail(String subject, String content, String from, String[] to) throws Exception {
        log.info("要发邮件了！");
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //是否发送的邮件是富文本（附件，图片，html等）
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        messageHelper.setFrom(from);
        messageHelper.setTo(to);
        messageHelper.setSubject(subject);
        //重点,默认为false，显示原始html代码，无效果;为true时可识别html代码，有效果
        messageHelper.setText(content, true);
        //发送邮件
        log.info("发送邮件开始！");
        javaMailSender.send(mimeMessage);
        log.info("邮件发送成功,subject:{},form:{},to:{},content:{}", subject, from, to, content);
    }

    /**
     * 附件
     *
     * @param subject
     * @param content
     * @param attachmentMap
     * @param from
     * @param to
     * @throws MessagingException
     */
    public static void sendAttachmentMail(String subject, String content, String from, String[] to, Map<String, String> attachmentMap) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //是否发送的邮件是富文本（附件，图片，html等）
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        messageHelper.setFrom(from);
        messageHelper.setTo(to);
        messageHelper.setSubject(subject);
        //重点，默认为false，显示原始html代码，无效果
        messageHelper.setText(content);
        if (attachmentMap != null) {
            attachmentMap.entrySet().stream().forEach(entrySet -> {
                try {
                    File file = new File(entrySet.getValue());
                    if (file.exists()) {
                        messageHelper.addAttachment(entrySet.getKey(), new FileSystemResource(file));
                    }
                } catch (MessagingException e) {
                    e.printStackTrace();
                }
            });
        }
        //发送邮件
        javaMailSender.send(mimeMessage);
        log.info("邮件发送成功,subject:{},form:{},to:{}", subject, from, to);
    }

    /**
     * 附件2------流式发送
     *
     * @param subject
     * @param content
     * @param from
     * @param to
     * @param inputStreamSource
     * @param fileName
     * @throws MessagingException
     */
    public static void sendAttachmentMail2(String subject, String content, String from, String[] to, InputStreamSource inputStreamSource, String fileName) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //是否发送的邮件是富文本（附件，图片，html等）
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        messageHelper.setFrom(from);
        messageHelper.setTo(to);
        messageHelper.setSubject(subject);
        //重点，默认为false，显示原始html代码，无效果
        messageHelper.setText(content);
        messageHelper.addAttachment(fileName, inputStreamSource);
        //发送邮件
        javaMailSender.send(mimeMessage);
        log.info("邮件发送成功,subject:{},form:{},to:{}", subject, from, to);
    }


    public static boolean isEmail(String email) {
        if (null == email || "".equals(email)) {
            return false;
        }
        String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p = Pattern.compile(regEx1);
        Matcher m = p.matcher(email);
        if (m.matches()) {
            return true;
        } else {
            return false;
        }
    }

   /* public static void main(String[] args) {
        String msg = "1dasd23424@qq。cmn";
        System.out.println(isEmail(msg));
    }*/

}
